package com.xxh.workQueue;

import com.rabbitmq.client.*;
import com.xxh.utils.MqUtils;

import java.io.IOException;

public class Consumer2 {
    public static void main(String[] args) throws IOException {
        Connection connection = MqUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("testwork", false, false, false, null);
        //一次消费一个消息
        channel.basicQos(1);
        /**
         * 1 队列名称
         * 2 是否自动确认
         * 3 消息的回调函数
         */
        channel.basicConsume("testwork", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("我是消费者2：" + new String(body));
                /**
                 * 1 确认消息是哪一条？
                 * 2 一次确认多个消息？FALSE就是每次确认一个消息
                 */
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }
}
